import java.awt.GridLayout;
import java.awt.BorderLayout;
import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JList;
import javax.swing.JLabel;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.math.BigDecimal;
import java.util.GregorianCalendar;
import java.sql.Date;
import java.util.Vector;

/**
 * MainWindow.java
 * 
 * Provides the primary Graphical Interface for the user to interact with the club database project
 * 
 * @author Steven Glazer
 * @author Kyle Marcotte
 * @author Sam Milton
 */
public class MainWindow extends JFrame {
	
	private static final long serialVersionUID = 1L;

	//Class Variables
	private final int HGAP = 12;
	private final int VGAP = 12;
	private final int DISP_NONE = -1;
	private final int DISP_FINANCES = 0;
	private final int DISP_EVENTS = 1;
	private final int DISP_DISCIPLINE = 2;
	
	private Vector<String> students = new Vector<String>();
	private Vector<String> clubs = new Vector<String>();
	private StudentInfo[] studentInfo;
	private Club[] clubInfo;
	private int displayMode = DISP_NONE;
	private MainWindow self = this;
	
	private JPanel pane;
	private JTextArea infoList;
	private JScrollPane infoListScroll;
	private JScrollPane studentListScroll;
	private JScrollPane clubListScroll;
	private JList studentList;
	private JList clubList;
	private JTabbedPane tabPane;
	
	private JPanel studentPanel;
	private JPanel clubPanel;
	private JPanel centerPanel;
	private JPanel infoPanel;
	private JPanel buttonPanel;
	private JPanel studentButPanel;
	private JPanel clubButPanel;
	
	private JButton financesBut;
	private JButton eventsBut;
	private JButton disciplineBut;
	private JButton membersBut;
	private JButton addBut;
	private JButton addBudgetBut;
	private JButton addStudentBut;
	private JButton removeStudentBut;
	private JButton assignStudentBut;
	private JButton unassignStudentBut;
	private JButton addClubBut;
	private JButton removeClubBut;
	private JButton assignClubBut;

	private JComboBox quarterCombo;
	
	private JLabel infoLabel1, infoLabel2, infoLabel3, infoLabel4;
	
	
	/**
	 * Constructor - Builds Main Window and Initializes Values
	 */
	public MainWindow() {
		this.setTitle("RIT Club Info System");
		pane = (JPanel)this.getContentPane();
		pane.setLayout(new BorderLayout(HGAP, VGAP));
		pane.setBorder(BorderFactory.createEmptyBorder(VGAP, HGAP, VGAP, HGAP));
		
		infoList = new JTextArea();
		infoList.setEditable(false);
		infoList.setLineWrap(true);
		infoListScroll = new JScrollPane(infoList);
		
		studentPanel = new JPanel();
		studentPanel.setLayout(new BorderLayout());
		clubPanel = new JPanel();
		clubPanel.setLayout(new BorderLayout());
		centerPanel = new JPanel();
		centerPanel.setLayout(new GridLayout(3, 1, HGAP, VGAP));
		infoPanel = new JPanel();
		infoPanel.setLayout(new GridLayout(3,3));
		infoPanel.setBorder(BorderFactory.createTitledBorder("Information:"));
		buttonPanel = new JPanel();
		buttonPanel.setLayout(new GridLayout(10, 1, HGAP, VGAP));
		studentButPanel = new JPanel();
		studentButPanel.setLayout(new GridLayout(4, 1));
		clubButPanel = new JPanel();
		clubButPanel.setLayout(new GridLayout(3, 1));
		
		tabPane = new JTabbedPane();
		tabPane.addTab("Students", studentPanel);
		tabPane.addTab("Clubs", clubPanel);
		
		// JLabels
		infoLabel1 = new JLabel("");
		infoLabel2 = new JLabel("");
		infoLabel3 = new JLabel("");
		infoLabel4 = new JLabel("");
		
		// Buttons
		financesBut = new JButton("Finances");
		eventsBut = new JButton("Events");
		disciplineBut = new JButton("Discipline");
		membersBut = new JButton("Members");
		addBut = new JButton("Add");
		addBudgetBut = new JButton("Add Budget");
		addStudentBut = new JButton("Add Student");
		removeStudentBut = new JButton("Remove Student");
		assignStudentBut = new JButton("Assign Club");
		unassignStudentBut = new JButton("Unassign Club");
		addClubBut = new JButton("Add Club");
		removeClubBut = new JButton("Remove Club");
		assignClubBut = new JButton("Assign Discipline");
		
		quarterCombo = new JComboBox(BudgetModel.getQuarters());
		
		// JList Setup
		studentList = new JList();
		studentListScroll = new JScrollPane(studentList);
		clubList = new JList();
		clubListScroll = new JScrollPane(clubList);
		
		// Adding to Panels
		studentButPanel.add(addStudentBut);
		studentButPanel.add(removeStudentBut);
		studentButPanel.add(assignStudentBut);
		studentButPanel.add(unassignStudentBut);
		clubButPanel.add(addClubBut);
		clubButPanel.add(removeClubBut);
		clubButPanel.add(assignClubBut);
		
		studentPanel.add(studentListScroll, BorderLayout.CENTER);
		studentPanel.add(studentButPanel, BorderLayout.EAST);
		clubPanel.add(clubListScroll, BorderLayout.CENTER);
		clubPanel.add(clubButPanel, BorderLayout.EAST);
		
		buttonPanel.add(financesBut);
		buttonPanel.add(eventsBut);
		buttonPanel.add(disciplineBut);
		buttonPanel.add(membersBut);
		buttonPanel.add(addBut);
		buttonPanel.add(addBudgetBut);
		buttonPanel.add(new JLabel(""));
		buttonPanel.add(new JLabel(""));
		buttonPanel.add(new JLabel(""));
		buttonPanel.add(quarterCombo);
		
		centerPanel.add(tabPane);
		centerPanel.add(infoPanel);
		centerPanel.add(infoListScroll);
		
		infoPanel.add(infoLabel1);
		infoPanel.add(infoLabel2);
		infoPanel.add(infoLabel3);
		infoPanel.add(infoLabel4);
		
		pane.add(centerPanel, BorderLayout.CENTER);
		pane.add(buttonPanel, BorderLayout.EAST);
		
		// Initialize Buttons/Labels to disabled
		financesBut.setEnabled(false);
		eventsBut.setEnabled(false);
		disciplineBut.setEnabled(false);
		membersBut.setEnabled(false);
		addBut.setEnabled(false);
		addBudgetBut.setVisible(false);
		quarterCombo.setVisible(false);
				
		// Setup Window and Show
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setSize(800,600);
		this.setLocation(300, 100);
		this.setVisible(true);
		
		// Initialize Values
		studentInfo = StudentModel.getStudents();
		for (StudentInfo student : studentInfo) {
			students.add(student.first + " " + student.last);
		}
		studentList.setListData(students);
		studentList.setSelectedIndex(0);
		
		int index = studentList.getSelectedIndex();
		infoLabel1.setText("Email: " + studentInfo[index].email);
		infoLabel2.setText("UID: " + studentInfo[index].uid);
		self.displayStudentClubs();
		
		clubInfo = ClubModel.getClubs();
		if (clubInfo == null)
			JOptionPane.showMessageDialog(self, "Could not get club info", 
					"Club Information Warning", JOptionPane.WARNING_MESSAGE);
			
		for (Club club : clubInfo) {
			clubs.add(club.name);
		}
		clubList.setListData(clubs);
		clubList.setSelectedIndex(0);
		
		// Listeners
		tabPane.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent evt) {
				if (tabPane.getSelectedIndex() == 0) {
					financesBut.setEnabled(false);
					eventsBut.setEnabled(false);
					disciplineBut.setEnabled(false);
					membersBut.setEnabled(false);
					addBut.setEnabled(false);
					addBudgetBut.setVisible(false);
					quarterCombo.setVisible(false);
					
					int index = studentList.getSelectedIndex();
					if (index != -1) {
						infoLabel1.setText("Email: " + studentInfo[index].email);
						infoLabel2.setText("UID: " + studentInfo[index].uid);
						infoLabel3.setText("");
						self.displayStudentClubs();
					}
				}
				else {
					int index = clubList.getSelectedIndex();
					if (index != -1) {
						infoLabel1.setText("Club ID: " + clubInfo[index].id);
						infoLabel2.setText("Club Rank: " + clubInfo[index].rank);
						infoLabel3.setText("Club Type: " + clubInfo[index].type);
						self.clubSelectionChange();
					}
				}
			}
		});
		
		financesBut.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				self.displayFinances();
			}
		});
		
		eventsBut.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				self.displayEvents();
			}
		});
		
		disciplineBut.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				self.displayDiscipline();
			}
		});
		
		membersBut.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				self.displayMembers();
			}
		});
	
		addBut.addActionListener(new ActionListener () {
			public void actionPerformed(ActionEvent e) {
				int index = clubList.getSelectedIndex();
				if (displayMode == DISP_FINANCES && index != -1) {
					AddFinanceWindow afw = new AddFinanceWindow(self, true);
					String[] newFinance = afw.getResult();
					if (newFinance != null) {
						int quarter = ((Integer)quarterCombo.getSelectedItem()).intValue();
						BigDecimal amount = new BigDecimal(newFinance[3]);
						GregorianCalendar cal = new GregorianCalendar();
						cal.set(Integer.parseInt(newFinance[0].split("/")[2]),
								Integer.parseInt(newFinance[0].split("/")[0]) - 1,
								Integer.parseInt(newFinance[0].split("/")[1]));
						try {
							if(!BudgetModel.addFinance(clubInfo[index].id, quarter, 
									new Date(cal.getTimeInMillis()), newFinance[1], newFinance[2], amount))
								JOptionPane.showMessageDialog(self, "Could not add finance", 
										"Finance Addition Warning", JOptionPane.WARNING_MESSAGE);
							else {
								self.displayFinances();
							}
						} catch (FinanceException e1) {
							JOptionPane.showMessageDialog(self, e1.getMessage(), 
									"Finance Addition Warning", JOptionPane.WARNING_MESSAGE);
						}
					}
				}
				else if (displayMode == DISP_EVENTS && index != -1) {
					AddEventWindow aew = new AddEventWindow(self, true);
					String[] info = aew.getResult();
					if (info != null) {
						Vector<EventDate> starts = aew.getStartTimes();
						Vector<EventDate> ends = aew.getEndTimes();
						double cost = Double.parseDouble(info[2]);
						if (!EventModel.addEvent(clubInfo[index].id, info[0], info[1], cost))
							JOptionPane.showMessageDialog(self, "Could not add event: " + info[0], 
									"Event Addition Warning", JOptionPane.WARNING_MESSAGE);
						else {
							for (int i = 0; i < starts.size(); i++) {
								if (!EventModel.addTime(info[0], starts.get(i), ends.get(i))) {
									JOptionPane.showMessageDialog(self, "Could not add time to event", 
											"Event Addition Warning", JOptionPane.WARNING_MESSAGE);
								}
							}
						}
					}
					self.displayEvents();
				}
				else if (displayMode == DISP_DISCIPLINE && index != -1) {
					AddDisciplineWindow adw = new AddDisciplineWindow(self, true);
					String[] newDiscipline = adw.getResult();
					if(newDiscipline != null) {
						if (!DisciplineModel.addDiscipline(Integer.parseInt(newDiscipline[0]),
								newDiscipline[1], newDiscipline[2]))
							JOptionPane.showMessageDialog(self, "Could not add discipline", 
									"Discipline Addition Warning", JOptionPane.WARNING_MESSAGE);
						else {
							self.displayDiscipline();
						}
					}
				}
			}
		});
		
		addBudgetBut.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int index = clubList.getSelectedIndex();
				if (index != -1) {
					int quarterId = ((Integer)quarterCombo.getSelectedItem()).intValue();
					AddBudgetWindow abw = new AddBudgetWindow(self, true);
					String a = abw.getResult();
					if (a != null) {
						BigDecimal avail = new BigDecimal(a);
						if(!BudgetModel.addBudget(clubInfo[index].id, quarterId, avail, new BigDecimal(0.0)))
							JOptionPane.showMessageDialog(self, "Could not add budget", 
									"Budget Addition Warning", JOptionPane.WARNING_MESSAGE);
						else {
							self.displayFinances();
						}
					}
				}
			}
		});
		
		studentList.addListSelectionListener(new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent e) {
				int index = studentList.getSelectedIndex();
				if (index != -1) {
					infoLabel1.setText("Email: " + studentInfo[index].email);
					infoLabel2.setText("UID: " + studentInfo[index].uid);
					self.displayStudentClubs();
				}
			}
		});
		
		clubList.addListSelectionListener(new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent e) {
				int index = clubList.getSelectedIndex();
				if (index != -1) {
					infoLabel1.setText("Club ID: " + clubInfo[index].id);
					infoLabel2.setText("Club Rank: " + clubInfo[index].rank);
					infoLabel3.setText("Club Type: " + clubInfo[index].type);
					self.clubSelectionChange();
				}
			}
		});
		
		removeStudentBut.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int index = studentList.getSelectedIndex();
				if (!StudentModel.removeStudent(studentInfo[index].uid))
					JOptionPane.showMessageDialog(self, "Could not remove: " + studentInfo[index].first + " " + 
							studentInfo[index].last, "Student Deletion Warning", JOptionPane.WARNING_MESSAGE);
				else {
					studentInfo = StudentModel.getStudents();
					students.clear();
					for (StudentInfo student : studentInfo) {
						students.add(student.first + " " + student.last);
					}
					studentList.setModel(new DefaultListModel());
					studentList.setListData(students);
					if (index-1 >= 0)
						studentList.setSelectedIndex(index-1);
					else
						studentList.setSelectedIndex(0);
				}
			}
		});
		
		addStudentBut.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				AddStudentWindow asw = new AddStudentWindow(self, true);
				String[] newStudent = asw.getResult();
				if(newStudent != null) {
					if(!StudentModel.addStudent(Integer.parseInt(newStudent[2]), newStudent[0], 
						newStudent[1], newStudent[3]))
						JOptionPane.showMessageDialog(self, "Could not add: " + newStudent[0] + " " + newStudent[1],
								"Student Addition Warning", JOptionPane.WARNING_MESSAGE);
					else {
						studentInfo = StudentModel.getStudents();
						students.clear();
						for (StudentInfo student : studentInfo) {
							students.add(student.first + " " + student.last);
						}
						studentList.setModel(new DefaultListModel());
						studentList.setListData(students);
						studentList.setSelectedIndex(students.indexOf(newStudent[0] + " " + newStudent[1]));
					}
				}
			}
		});
		
		assignStudentBut.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int index = studentList.getSelectedIndex();
				if (index != -1) {
					String[] data = new String[clubInfo.length];
					for(int i = 0; i < clubInfo.length; i++) {
						data[i] = clubInfo[i].name;
					}
					AssignClubWindow acw = new AssignClubWindow(self, true, data);
					int clubIndex = acw.getResult();
					String position = acw.getPosition();
					if (position != null && clubIndex != -1) {
						if (!ClubModel.addMember(studentInfo[index], clubInfo[clubIndex], position))
							JOptionPane.showMessageDialog(self, "Could not assign club", 
									"Club Assign Warning", JOptionPane.WARNING_MESSAGE);
					}
					else if (clubIndex != -1) {
						if (!ClubModel.addMember(studentInfo[index], clubInfo[clubIndex]))
							JOptionPane.showMessageDialog(self, "Could not assign club", 
									"Club Assign Warning", JOptionPane.WARNING_MESSAGE);
					}
					self.displayStudentClubs();
				}
			}
		});
		
		unassignStudentBut.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int index = studentList.getSelectedIndex();
				if (index != -1) {
					ClubMember[] memberClubs = ClubModel.getClubsOfMember(studentInfo[index].uid);
					if (memberClubs.length > 0 || memberClubs == null) {
						String[] data = new String[memberClubs.length];
						for (int i = 0; i < memberClubs.length; i++) {
							data[i] = memberClubs[i].club.name;
						}
						UnassignClubWindow ucw = new UnassignClubWindow(self, true, data);
						int clubIndex = ucw.getResult();
						if (clubIndex != -1) {
							if(!ClubModel.removeMember(studentInfo[index], memberClubs[clubIndex].club))
								JOptionPane.showMessageDialog(self, "Could not unassign club",
										"Club Unassign Warning", JOptionPane.WARNING_MESSAGE);
							else {
								self.displayStudentClubs();
							}
						}
					}
					else
						JOptionPane.showMessageDialog(self, "No Clubs to Unassign",
								"Not a Member Warning", JOptionPane.WARNING_MESSAGE);
				}
			}
		});
		
		removeClubBut.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int index = clubList.getSelectedIndex();
				if (index != -1) {
					if(!ClubModel.removeClub(clubInfo[index].id))
						JOptionPane.showMessageDialog(self, 
								"Could not remove: " + clubInfo[index].name,
								"Club Deletion Warning", JOptionPane.WARNING_MESSAGE);
					else {
						clubInfo = ClubModel.getClubs();
						if (clubInfo != null) {
							clubs.clear();
							for (Club c : clubInfo) {
								clubs.add(c.name);
							}
							clubList.setModel(new DefaultListModel());
							clubList.setListData(clubs);
							if (index - 1 < 0)
								clubList.setSelectedIndex(0);
							else
								clubList.setSelectedIndex(index-1);
						}
						else
							JOptionPane.showMessageDialog(self, 
									"Could not get club information",
									"Club Info Warning", JOptionPane.WARNING_MESSAGE);
					}
				}
			}
		});
		
		addClubBut.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				AddClubWindow acw = new AddClubWindow(self, true);
				String[] newClub = acw.getResult();
				if (newClub != null) {
					if (!ClubModel.addClub(newClub[0], newClub[1], newClub[2]))
						JOptionPane.showMessageDialog(self, "Could not add: " + newClub[0], "Club Addition Warning", 
								JOptionPane.WARNING_MESSAGE);
					else {
						clubInfo = ClubModel.getClubs();
						if (clubInfo != null) {
							clubs.clear();
							for (Club club : clubInfo) {
								clubs.add(club.name);
							}	
							clubList.setModel(new DefaultListModel());
							clubList.setListData(clubs);
							clubList.setSelectedIndex(clubs.indexOf(newClub[0]));
						}
						else
							JOptionPane.showMessageDialog(self, "Could not get club information", 
									"Club Info Warning", JOptionPane.WARNING_MESSAGE);
					}
				}
			}
		});
		
		quarterCombo.addActionListener( new ActionListener() { 
			public void actionPerformed(ActionEvent e) {
				self.displayFinances();
			}
		});
		
		assignClubBut.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int index = clubList.getSelectedIndex();
				if (index != -1) {
					Discipline[] disciplines = DisciplineModel.getDisciplines();
					AssignDisciplineWindow adw = new AssignDisciplineWindow(self, true, disciplines);
					int discIndex = adw.getResult();
					if (discIndex != -1) {
						String[] assign = adw.getInfo();
						String reason = assign[0];
						String date = assign[1];
						GregorianCalendar cal = new GregorianCalendar();
						cal.set(Integer.parseInt(date.split("/")[2]),
								Integer.parseInt(date.split("/")[0]) - 1,
								Integer.parseInt(date.split("/")[1]));
						if (!DisciplineModel.disciplineClub(disciplines[discIndex].code,
								clubInfo[index], reason, new Date(cal.getTimeInMillis())))
							JOptionPane.showMessageDialog(self, "Could not assign discipline", 
									"Club Discipline Warning", JOptionPane.WARNING_MESSAGE);
						else {
							self.clubSelectionChange();
						}
					}
				}
			}
		});
	}
	
	/**
	 * Sets the text in the infoList to the given String
	 * 
	 * @param message	String to be displayed
	 */
	private void setInfoList(String message) {
		infoList.setText(message + "\n");
		infoList.setCaretPosition(infoList.getText().length());
	}
	
	/**
	 * Appends the given String to the text in the infoList
	 * 
	 * @param message	String to be appended
	 */
	private void appendInfoList(String message) {
		infoList.append(message + "\n");
		infoList.setCaretPosition(infoList.getText().length());
	}
	
	/**
	 * Determines which method to call when a new selection is made 
	 * in the clubList view. Determined by displayMode
	 */
	private void clubSelectionChange() {
		if (displayMode == DISP_NONE)
			self.displayMembers();
		else if (displayMode == DISP_FINANCES)
			self.displayFinances();
		else if (displayMode == DISP_EVENTS)
			self.displayEvents();
		else
			self.displayDiscipline();
	}
	
	/**
	 * Displays the club information for the selected student
	 */
	private void displayStudentClubs() {
		self.setInfoList("Clubs/Positions:\n");
		int index = studentList.getSelectedIndex();
		if (index != -1) {
			ClubMember[] members = ClubModel.getClubsOfMember(studentInfo[index]);
			for (ClubMember cm : members) {
				if (cm.position != null)
					self.appendInfoList(cm.joined + "\t" + cm.club.name + "\n" + cm.position + "\n");
				else
					self.appendInfoList(cm.joined + "\t" + cm.club.name + "\n");
			}
		}
	}
	
	/**
	 * Displays the members for the selected club
	 */
	private void displayMembers() {
		int index = clubList.getSelectedIndex();
		membersBut.setEnabled(false);
		disciplineBut.setEnabled(true);
		eventsBut.setEnabled(true);
		financesBut.setEnabled(true);
		addBut.setEnabled(false);
		addBudgetBut.setVisible(false);
		quarterCombo.setVisible(false);
		displayMode = DISP_NONE;
		self.setInfoList("Members:\n");
		if (index != -1) {
			ClubMember[] clubMembers = ClubModel.getClubMembers(clubInfo[index]);
			if (clubMembers != null) {
				for(ClubMember memb : clubMembers) {
					if (memb.position != null)
						self.appendInfoList(memb.joined + "\t" + memb.member.first + " " + 
								memb.member.last + "\n" + memb.position +"\n");
					else
						self.appendInfoList(memb.joined + "\t" + memb.member.first + " " + 
								memb.member.last + "\n");
				}
			}
			else
				JOptionPane.showMessageDialog(self, "Could not retrieve club member information", 
						"Member Retrieval Warning", JOptionPane.WARNING_MESSAGE);
		}
	}
	
	/**
	 * Displays the disciplines for the selected club
	 */
	private void displayDiscipline() {
		membersBut.setEnabled(true);
		disciplineBut.setEnabled(false);
		eventsBut.setEnabled(true);
		financesBut.setEnabled(true);
		addBut.setEnabled(true);
		addBudgetBut.setVisible(false);
		quarterCombo.setVisible(false);
		displayMode = DISP_DISCIPLINE;
		self.setInfoList("Discipline:\n");
		int index = clubList.getSelectedIndex();
		if (index != -1) {
			ClubDiscipline[] disciplines = DisciplineModel.getDisciplines(clubInfo[index]);
			for (ClubDiscipline d : disciplines) {
				self.appendInfoList(d.code + "\t" + d.expires.toString() + "\t\t" + d.reason);
			}
		}
	}
	
	/**
	 * Displays the events for the selected club
	 */
	private void displayEvents() {
		int index = clubList.getSelectedIndex();
		membersBut.setEnabled(true);
		disciplineBut.setEnabled(true);
		eventsBut.setEnabled(false);
		financesBut.setEnabled(true);
		addBut.setEnabled(true);
		addBudgetBut.setVisible(false);
		quarterCombo.setVisible(false);
		displayMode = DISP_EVENTS;
		self.setInfoList("Events:\n");
		if (index != -1) {
			Event[] events = EventModel.getEvents(clubInfo[index].id);
			for (Event e : events) {
				self.appendInfoList(e.toString());
			}
		}
	}
	
	/**
	 * Displays the finances for the selected club
	 */
	private void displayFinances() {
		membersBut.setEnabled(true);
		disciplineBut.setEnabled(true);
		eventsBut.setEnabled(true);
		financesBut.setEnabled(false);
		quarterCombo.setVisible(true);
		displayMode = DISP_FINANCES;
		int qId = ((Integer)quarterCombo.getSelectedItem()).intValue();
		self.setInfoList("Finances:");
		self.appendInfoList("\nQuarter - " + qId);
		int index = clubList.getSelectedIndex();
		if (index != -1) {
			Budget b = BudgetModel.getBudget(clubInfo[index].id, qId);
			if (b != null) {
				addBudgetBut.setVisible(false);
				addBut.setEnabled(true);
				self.appendInfoList("Available: " + b.available + "\t\tUsed: " + b.used + "\n");
				Finance[] finances = BudgetModel.getFinances(clubInfo[index].id);
				if (finances != null) {
					for(Finance fin : finances) {
						self.appendInfoList("" + fin.date + "\t" + fin.description + "\t" + 
								fin.location + "\t" + fin.amount);
					}
				}
				else
					JOptionPane.showMessageDialog(self, "Could not retrieve finance information", 
							"Finance Retrieval Warning", JOptionPane.WARNING_MESSAGE);
			}
			else {
				addBudgetBut.setVisible(true);
				addBut.setEnabled(false);
				self.appendInfoList("No Budget for this quarter");
			}
		}
	}
	
	/**
	 * Initialize the Database and Create Main Window
	 */
	public static void main(String[] args) {
		try {
			DB.Initialize();
			new MainWindow();
			
		} catch (DBError e) {
			System.err.println( e.getMessage() );
			e.printStackTrace();
		}
	}

}
